package com.jushu.storbox.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import com.jushu.storbox.entities.TeamSpaceInfo;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.modelV2.response.TeamSpaceEntity;

public class TeamSpaceDao extends BaseDao implements ITeamSpaceDao {
	public final static String TABLE_NAME = "tb_team_space_list";
	private static final String LOG_TAG = "TeamSpaceDao";

	@Override
	public void insertTeamSpace(TeamSpaceEntity teamSpaceInfo,
			String memberShipsId, String teamRole) {
		// TODO Auto-generated method stub
		try {
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.teamspace_id, teamSpaceInfo.getId());
			cv.put(IBaseDao.name, teamSpaceInfo.getName());
			cv.put(IBaseDao.description, teamSpaceInfo.getDescription());
			cv.put(IBaseDao.current_numbers, teamSpaceInfo.getCurNumbers());
			cv.put(IBaseDao.created_at, teamSpaceInfo.getCreatedAt());
			cv.put(IBaseDao.created_by, teamSpaceInfo.getCreatedBy());
			cv.put(IBaseDao.created_by_name,
					teamSpaceInfo.getCreatedByUserName());
			cv.put(IBaseDao.owner_by, teamSpaceInfo.getOwnerBy());
			cv.put(IBaseDao.ownerName, teamSpaceInfo.getOwnedByUserName());
			cv.put(IBaseDao.status, teamSpaceInfo.getStatus());
			cv.put(IBaseDao.space_quota, teamSpaceInfo.getSpaceQuota());
			cv.put(IBaseDao.space_used, teamSpaceInfo.getSpaceUsed());
			cv.put(IBaseDao.max_version, teamSpaceInfo.getMaxVersion());
			cv.put(IBaseDao.max_members, teamSpaceInfo.getMaxMembers());
			cv.put(IBaseDao.memberships_id, memberShipsId);
			cv.put(IBaseDao.team_role, teamRole);
			this.getDb().insert(TABLE_NAME, null, cv);

		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"insertTeamSpace is fail:" + e.getLocalizedMessage());
		}

	}

	@Override
	public void insertTeamSpace(TeamSpaceInfo teamSpaceInfo) {
		// TODO Auto-generated method stub
		try {
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.teamspace_id, teamSpaceInfo.getTeamSpaceId());
			cv.put(IBaseDao.name, teamSpaceInfo.getName());
			cv.put(IBaseDao.description, teamSpaceInfo.getDescription());
			cv.put(IBaseDao.current_numbers, teamSpaceInfo.getCurNumbers());
			cv.put(IBaseDao.created_at, teamSpaceInfo.getCreatedAt());
			cv.put(IBaseDao.created_by, teamSpaceInfo.getCreatedBy());
			cv.put(IBaseDao.created_by_name,
					teamSpaceInfo.getCreatedByUserName());
			cv.put(IBaseDao.owner_by, teamSpaceInfo.getOwerBy());
			cv.put(IBaseDao.ownerName, teamSpaceInfo.getOwnerByUserName());
			cv.put(IBaseDao.status, teamSpaceInfo.getStatus());
			cv.put(IBaseDao.space_quota, teamSpaceInfo.getSpaceQuota());
			cv.put(IBaseDao.space_used, teamSpaceInfo.getSpaceUsed());
			cv.put(IBaseDao.max_version, teamSpaceInfo.getMaxVersion());
			cv.put(IBaseDao.max_members, teamSpaceInfo.getMaxMembers());
			cv.put(IBaseDao.memberships_id, teamSpaceInfo.getMemberShipsId());
			cv.put(IBaseDao.team_role, teamSpaceInfo.getTeamRole());
			this.getDb().insert(TABLE_NAME, null, cv);

		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"insertTeamSpace is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public void updateTeamSpace(TeamSpaceEntity teamSpaceInfo) {
		// TODO Auto-generated method stub

		try {
			String where = teamspace_id + "=?";
			String[] whereValue = { teamSpaceInfo.getId() };
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.teamspace_id, teamSpaceInfo.getId());
			cv.put(IBaseDao.name, teamSpaceInfo.getName());
			cv.put(IBaseDao.description, teamSpaceInfo.getDescription());
			cv.put(IBaseDao.current_numbers, teamSpaceInfo.getCurNumbers());
			cv.put(IBaseDao.created_at, teamSpaceInfo.getCreatedAt());
			cv.put(IBaseDao.created_by, teamSpaceInfo.getCreatedBy());
			cv.put(IBaseDao.created_by_name,
					teamSpaceInfo.getCreatedByUserName());
			cv.put(IBaseDao.owner_by, teamSpaceInfo.getOwnerBy());
			cv.put(IBaseDao.ownerName, teamSpaceInfo.getOwnedByUserName());
			cv.put(IBaseDao.status, teamSpaceInfo.getStatus());
			cv.put(IBaseDao.space_quota, teamSpaceInfo.getSpaceQuota());
			cv.put(IBaseDao.space_used, teamSpaceInfo.getSpaceUsed());
			cv.put(IBaseDao.max_version, teamSpaceInfo.getMaxVersion());
			cv.put(IBaseDao.max_members, teamSpaceInfo.getMaxMembers());
			this.getDb().update(TABLE_NAME, cv, where, whereValue);

		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateTeamSpace is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public void deleteTeamSpace(String teamSpaceId) {
		// TODO Auto-generated method stub
		try {
			String where = teamspace_id + "=?";
			String[] whereValue = { teamSpaceId };
			this.getDb().delete(TABLE_NAME, where, whereValue);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"deleteTeamSpace is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public List<TeamSpaceInfo> getTeamSpacesList() {
		List<TeamSpaceInfo> teamSpacesList = new ArrayList<TeamSpaceInfo>();
		try {
			Cursor cursor = this.getDb().rawQuery(
					"select * from " + TABLE_NAME + " order by " + IBaseDao.created_at + " desc", null);
			if (false == cursor.moveToFirst()) {
				return teamSpacesList;
			}

			do {
				TeamSpaceInfo teamSpaceInfo = new TeamSpaceInfo();
				teamSpaceInfo.setTeamSpaceId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.teamspace_id)));
				teamSpaceInfo.setName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.name)));
				teamSpaceInfo.setDescription(cursor.getString(cursor
						.getColumnIndex(IBaseDao.description)));
				teamSpaceInfo.setCurNumbers(cursor.getInt(cursor
						.getColumnIndex(IBaseDao.current_numbers)));
				teamSpaceInfo.setCreatedAt(cursor.getLong(cursor
						.getColumnIndex(IBaseDao.created_at)));
				teamSpaceInfo.setCreatedBy(cursor.getString(cursor
						.getColumnIndex(IBaseDao.created_by)));
				teamSpaceInfo.setCreatedByUserName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.created_by_name)));
				teamSpaceInfo.setOwerBy(cursor.getString(cursor
						.getColumnIndex(IBaseDao.owner_by)));
				teamSpaceInfo.setOwnerByUserName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.ownerName)));
				teamSpaceInfo.setStatus((byte) cursor.getInt(cursor
						.getColumnIndex(IBaseDao.status)));
				teamSpaceInfo.setSpaceQuota(cursor.getLong(cursor
						.getColumnIndex(IBaseDao.space_quota)));
				teamSpaceInfo.setSpaceUsed(cursor.getLong(cursor
						.getColumnIndex(IBaseDao.space_used)));
				teamSpaceInfo.setMaxVersion(cursor.getInt(cursor
						.getColumnIndex(IBaseDao.max_version)));
				teamSpaceInfo.setMaxMembers(cursor.getInt(cursor
						.getColumnIndex(IBaseDao.max_members)));
				teamSpaceInfo.setMemberShipsId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.memberships_id)));
				teamSpaceInfo.setTeamRole(cursor.getString(cursor
						.getColumnIndex(IBaseDao.team_role)));

				teamSpacesList.add(teamSpaceInfo);
			} while (cursor.moveToNext());
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getTeamSpaceList is fail:" + e.getLocalizedMessage());
		}
		return teamSpacesList;
	}

	@Override
	public TeamSpaceInfo getTeamSpace(String teamSpaceId) {
		// TODO Auto-generated method stub
		TeamSpaceInfo teamSpaceInfo = null;
		try {
			Cursor cursor = this.getDb().query(
					TABLE_NAME,
					new String[] { teamspace_id, name, description,
							current_numbers, created_at, created_by,
							created_by_name, owner_by, ownerName, status,
							space_quota, space_used, max_version, max_members,
							memberships_id, team_role }, teamspace_id + " = ?",
					new String[] { teamSpaceId }, null, null, null);
			if(cursor.moveToFirst()){
				teamSpaceInfo=new TeamSpaceInfo();
				teamSpaceInfo.setTeamSpaceId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.teamspace_id)));
				teamSpaceInfo.setName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.name)));
				teamSpaceInfo.setDescription(cursor.getString(cursor
						.getColumnIndex(IBaseDao.description)));
				teamSpaceInfo.setCurNumbers(cursor.getInt(cursor
						.getColumnIndex(IBaseDao.current_numbers)));
				teamSpaceInfo.setCreatedAt(cursor.getLong(cursor
						.getColumnIndex(IBaseDao.created_at)));
				teamSpaceInfo.setCreatedBy(cursor.getString(cursor
						.getColumnIndex(IBaseDao.created_by)));
				teamSpaceInfo.setCreatedByUserName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.created_by_name)));
				teamSpaceInfo.setOwerBy(cursor.getString(cursor
						.getColumnIndex(IBaseDao.owner_by)));
				teamSpaceInfo.setOwnerByUserName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.ownerName)));
				teamSpaceInfo.setStatus((byte) cursor.getInt(cursor
						.getColumnIndex(IBaseDao.status)));
				teamSpaceInfo.setSpaceQuota(cursor.getLong(cursor
						.getColumnIndex(IBaseDao.space_quota)));
				teamSpaceInfo.setSpaceUsed(cursor.getLong(cursor
						.getColumnIndex(IBaseDao.space_used)));
				teamSpaceInfo.setMaxVersion(cursor.getInt(cursor
						.getColumnIndex(IBaseDao.max_version)));
				teamSpaceInfo.setMaxMembers(cursor.getInt(cursor
						.getColumnIndex(IBaseDao.max_members)));
				teamSpaceInfo.setMemberShipsId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.memberships_id)));
				teamSpaceInfo.setTeamRole(cursor.getString(cursor
						.getColumnIndex(IBaseDao.team_role)));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getTeamSpace is fail:" + e.getLocalizedMessage());
		}
		return teamSpaceInfo;
	}

	@Override
	public long getLastUpdateTime(String objectId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void updateLastUpdateTime(String objectId) {
		// TODO Auto-generated method stub

	}

}
